package com.csd.serviceImpl;

import java.util.List;

import com.csd.dao.ManagerDAO;
import com.csd.exception.*;
import com.csd.helper.IConstant;
import com.csd.helper.SessionManager;
import com.csd.pojo.Manager;
import com.csd.service.IManagerService;
import com.csd.vo.UserVO;
/**
 * @version 1.0
 * @author sanmu，尹华彬 <br>
 *         创建于2009-12-17
 *         
 */
public class ManagerService implements IManagerService {

	public boolean updateManager(Manager manager) {
		// TODO Auto-generated method stub
		return false;
	}
	/**
	 * 重设管理员密码
	 * 
	 * oldPassword，newPassword
	 *            旧密码，新密码
	 * @return true为添加成功，false为失败
	 */
	public boolean resetPassword(String oldPassword, String newPassword) {
		boolean bl = false;
		try {
			UserVO vo = SessionManager.getUserVO();
			if(vo != null && vo.getType()==IConstant.managerRole)
			{
				Manager manager = managerDAO.findById(vo.getUserID());
				if(manager.getPassword().equals(oldPassword))
				{
					manager.setPassword(newPassword);
					managerDAO.updateManager(manager);
					bl = true;
				}
			}
			
		} catch (RuntimeException re) {
			throw new UpdateException(re, "重新设置密码异常");
		}
		return bl;
	}
  
	/**
	 * 列出所有的管理员
	 * 
	 * @param 
	 *            无
	 * @return list为所有的管理员
	 */
	public List<Manager> listAllManager() {
		List<Manager> list = null;
		try{
			list = managerDAO.findAll();
		} catch (RuntimeException re) {
			throw new GetException(re, "查询所有管理员异常");
		}
		return list;
	}

	private ManagerDAO managerDAO;

	public ManagerDAO getManagerDAO() {
		return managerDAO;
	}

	public void setManagerDAO(ManagerDAO managerDAO) {
		this.managerDAO = managerDAO;
	}
	
	/**
	 * 对管理员登陆的判断
	 * @param userVO
	 * @return uVO为登陆的用户
	 */
	public UserVO login(UserVO userVO) {
		UserVO uVO = null;
		
		try {
			Manager temp = null;
			List<Manager> list = null;
			list = managerDAO.findByUsername(userVO.getLoginName());
			if (!list.isEmpty() && list.size() == 1) {
				temp = list.get(0);
				if (userVO.getPassword().equals(temp.getPassword())) {
					uVO = new UserVO(temp.getId(),temp.getUsername(),IConstant.managerRole,temp.getName());				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			
		}
		return uVO;
	}
	
	/**
	 * 添加管理员
	 * @param manager
	 *     管理员实例
	 * @return true 为添加成功，false 为添加失败
	 */
	public boolean addManager(Manager manager){
		boolean bl = false;
		try {
			managerDAO.save(manager);
			bl = true;

		} catch (RuntimeException re) {
				throw new SaveException(re, "添加管理员异常");
			}
		return bl;
	}
	
	/**
	 * 删除管理员
	 * @param managerID
	 *   管理员ID
	 * @return true 为删除成功 false为删除失败
	 */
	public boolean deleteManager(Integer managerID)
	{
		Manager manager = new Manager();
		boolean bl = false;
		try {
			manager.setId(managerID);
			managerDAO.delete(manager);
			bl = true;
		} catch (RuntimeException re) {
			throw new DeleteException(re, "删除管理员异常");
		}
		return bl;
	}
	
	/**
	 *  通过ID 查找管理员
	 * @param ID
	 *       管理员ID
	 * @return 管理员实例
	 */


	public Manager getManagerByID(Integer ID) {
		Manager manager = null;

		try {
			manager = managerDAO.findById(ID);
		} catch (RuntimeException re) {
			throw new GetException(re, "查询管理员异常");
		}

		return manager;
	}



}
